﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using XChip_Common;
using XChipTool.Enties;

namespace XChipTool
{
    public partial class ConfigFile : Form
    {
        public bincurrencyManager Bincurrency_BLL;
        public delegate void SendEventBinSettingNewMsg(string msg);
        public event SendEventBinSettingNewMsg SendBinSetMsg;
        private string GuidNo;
        public ConfigFile()
        {
            InitializeComponent();
        }
        public ConfigFile(bincurrency_export binmodel)
        {
            GuidNo = binmodel.GuidId;
            InitializeComponent();
        }

        private void setbtnConfigFile0_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "TXT Files(*.txt;*.c)|*.txt;*.c|All Files(*.*)|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string strFilePath = openFileDialog1.FileName;
                //存储当前文件存放地址用于下一次生成
                AppHelper.UpdateAppConfig("Folder_Road_Force", Path.GetDirectoryName(strFilePath));
                PathConfigFile0.Text = strFilePath;
                var list = Bincurrency_BLL.GetList(c => c.GuidId == GuidNo && c.BinId == 2);
                if (list.Count == 0)
                {
                    bincurrency model = new bincurrency();
                    model.GuidId = GuidNo;
                    model.BinId = 2;
                    model.CreateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    model.BinInPath = strFilePath;
                    model.BinInName = Path.GetFileName(strFilePath);
                    var pathNameout = Copy_Files_For_Header_Patch_Tool(strFilePath, true, "", true, false);
                    model.BinOutName = Path.GetFileName(pathNameout);
                    model.BinOutPath = pathNameout;
                    model.FileStatus = 2;
                    model.SerialNo = 0;
                    model.SortOrder = 0;
                    model.IsModified = 1;
                    model.LengthAddress = HeaderHelper.FilePathLengthComplementnew(model.BinOutPath);
                    Bincurrency_BLL.Insert(model);
                }
                else
                {
                    var model = list.Find(c => c.GuidId == GuidNo && c.BinId == 2);
                    model.BinInPath = strFilePath;
                    model.BinInName = Path.GetFileName(strFilePath);
                    var pathNameout = Copy_Files_For_Header_Patch_Tool(strFilePath, true, "", true, false);
                    model.BinOutName = Path.GetFileName(pathNameout);
                    model.BinOutPath = pathNameout;
                    model.SerialNo = 0;
                    model.FileStatus = 2;
                    model.LengthAddress = HeaderHelper.FilePathLengthComplementnew(model.BinOutPath);
                    Bincurrency_BLL.Update(model);
                }
            }
            
        }

        private void setbtnConfigFile1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "TXT Files(*.txt;*.c)|*.txt;*.c|All Files(*.*)|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string strFilePath = openFileDialog1.FileName;
                //存储当前文件存放地址用于下一次生成
                AppHelper.UpdateAppConfig("Folder_Road_Force", Path.GetDirectoryName(strFilePath));
                PathConfigFile1.Text = strFilePath;
                var list = Bincurrency_BLL.GetList(c => c.GuidId == GuidNo && c.BinId == 3);
                if (list.Count == 0)
                {
                    bincurrency model = new bincurrency();
                    model.GuidId = GuidNo;
                    model.BinId = 3;
                    model.CreateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    model.BinInPath = strFilePath;
                    model.BinInName = Path.GetFileName(strFilePath);
                    var pathNameout = Copy_Files_For_Header_Patch_Tool(strFilePath, true, "", true, false);
                    model.BinOutName = Path.GetFileName(pathNameout);
                    model.BinOutPath = pathNameout;
                    model.FileStatus = 2;
                    model.SerialNo = 1;
                    model.SortOrder = 0;
                    model.IsModified = 0;
                    model.LengthAddress = HeaderHelper.FilePathLengthComplementnew(model.BinOutPath);
                    Bincurrency_BLL.Insert(model);
                    list = Bincurrency_BLL.GetList(c => c.GuidId == GuidNo && c.BinId == 2);
                    var mode = list.Find(c => c.IsModified == 1);
                    mode.FileStatus = 2;
                    Bincurrency_BLL.Update(mode);
                   
                }
                else
                {
                    var model = list.Find(c => c.GuidId == GuidNo && c.BinId == 3);
                    model.BinInPath = strFilePath;
                    model.BinInName = Path.GetFileName(strFilePath);
                    var pathNameout = Copy_Files_For_Header_Patch_Tool(strFilePath, true, "", true, false);
                    model.BinOutName = Path.GetFileName(pathNameout);
                    model.BinOutPath = pathNameout;
                    model.SerialNo = 1;
                    model.FileStatus = 2;
                    model.LengthAddress = HeaderHelper.FilePathLengthComplementnew(model.BinOutPath);
                    Bincurrency_BLL.Update(model);

                    list = Bincurrency_BLL.GetList(c => c.GuidId == GuidNo && c.BinId == 2);
                    var mode = list.Find(c => c.IsModified == 1);
                    mode.FileStatus = 2;
                    Bincurrency_BLL.Update(mode);
                }
            }
        }

        private void ConfigFile_Load(object sender, EventArgs e)
        {
            if (null == Bincurrency_BLL) //初始化
            {
                Bincurrency_BLL = new bincurrencyManager();
            }
            var list = Bincurrency_BLL.GetListToDefault(GuidNo);
            if (list.Any(item => item.BinId == 2))
            {
                PathConfigFile0.Text = list.Find(c => c.BinId == 2).BinInPath;
            }
            if (list.Any(item => item.BinId == 3))
            {
                PathConfigFile1.Text = list.Find(c => c.BinId == 3).BinInPath;
            }
            
        }
        public string Copy_Files_For_Header_Patch_Tool(string Inputfilepath, bool Isflash, string eepromaddr, bool isSetting, bool isHead)
        {
            string strFileName = System.IO.Path.GetFileName(Inputfilepath); //获取文件名
            string strNewSavePath = Application.StartupPath + "\\Lib\\xc151_setting\\Flash_" + strFileName;

            if (isHead)
            {
                Setting2FlashE2prom.FlashHeader2Flash(Inputfilepath, strNewSavePath);
            }
            else
            {
                //Setting2FlashE2prom.FlashSetting2Flash(Inputfilepath, strNewSavePath, 1, 0, 0);
                Setting2FlashE2prom.FlashSetting2Flash(Inputfilepath, strNewSavePath, 0, 0, 0);
            }

            return strNewSavePath;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            PathConfigFile0.Text = null;
            Bincurrency_BLL.Delete(c => c.GuidId == GuidNo && (c.BinId == 2));
            bincurrency model = new bincurrency();
            //model.AddressNo = "1000";
            //model.Address = "2000";
            model.BinId = 2;
            model.GuidId = GuidNo;
            model.IsModified = 1;
            model.FileStatus = 0;
            model.SerialNo = 0;
            model.SortOrder = 0;
            model.CreateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            Bincurrency_BLL.Insert(model);
            var list = Bincurrency_BLL.GetList(c => c.GuidId == GuidNo &&  c.BinId == 3);
            if (list.Count != 0)
            {
                list = Bincurrency_BLL.GetList(c => c.GuidId == GuidNo && c.BinId == 2);
                var mode = list.Find(c => c.IsModified == 1);
                mode.FileStatus = 2;
                Bincurrency_BLL.Update(mode);
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            PathConfigFile1.Text = null;
            Bincurrency_BLL.Delete(c => c.GuidId == GuidNo && (c.BinId == 3));
            var list = Bincurrency_BLL.GetList(c => c.GuidId == GuidNo && (c.BinId == 2 || c.BinId == 3) && c.BinInPath == null);
            if (list.Count != 0)
            {
                var mode = list.Find(c => c.IsModified == 1 );
                mode.FileStatus = 0;
                Bincurrency_BLL.Update(mode);
            }
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            if (null != SendBinSetMsg)
            {
                SendBinSetMsg("");
            }
            this.Close();
        }

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            if (null != SendBinSetMsg)
            {
                SendBinSetMsg("");
            }
            DialogResult dr = DevExpress.XtraEditors.XtraMessageBox.Show($"选择YES保存后退出！ \r\n 选择NO直接退出！", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
            if (dr == DialogResult.No)
            {
                Bincurrency_BLL.Delete(c => c.GuidId == GuidNo && (c.BinId == 2));
                Bincurrency_BLL.Delete(c => c.GuidId == GuidNo && (c.BinId == 3));
                bincurrency model = new bincurrency();
                //model.AddressNo = "1000";
                //model.Address = "2000";
                model.BinId = 2;
                model.GuidId = GuidNo;
                model.IsModified = 1;
                model.FileStatus = 0;
                model.SerialNo = 0;
                model.SortOrder = 0;
                model.CreateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                Bincurrency_BLL.Insert(model);
                this.Close();
            }
            else
            {
                this.Close();
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
    }
}
